<template>
    <div class="product-list">
      <h1>商品列表</h1>
  
      <!-- 悬浮分类菜单 -->
      <div class="floating-category">
        <span
          v-for="(category, index) in categories"
          :key="index"
          class="category-item"
          :class="{ active: selectedCategory === category }"
          @click="filterByCategory(category)"
        >
          {{ category }}
        </span>
      </div>
  
      <!-- 搜索框 -->
      <div class="search-box" :class="{ active: suggestions.length > 0 }">
        <input
          v-model="searchQuery"
          type="text"
          placeholder="搜索商品..."
          @input="filterProducts"
          class="search-input"
        />
        <!-- 提示框 -->
        <ul v-if="suggestions.length > 0" class="suggestions-list">
          <li
            v-for="(suggestion, index) in suggestions"
            :key="index"
            @click="selectSuggestion(suggestion)"
          >
            {{ suggestion.name }}
          </li>
        </ul>
      </div>
  
      <!-- 商品列表 -->
      <ul>
        <li v-for="product in paginatedProducts" :key="product.id" class="product-item">
          <!-- 商品图片 -->
          <img v-if="product.image" :src="product.image" :alt="product.name" class="product-image" />
          <div class="product-info">
            <h2>{{ product.name }}</h2>
            <p class="price">￥{{ product.price }}</p>
            <p class="category">分类：{{ product.category }}</p>
            <p class="stock">库存：{{ product.stock }}</p>
            <p class="description">{{ product.description }}</p>
          </div>
        </li>
      </ul>
  
      <!-- 分页控件 -->
      <div class="pagination">
        <button @click="changePage(currentPage - 1)" :disabled="currentPage <= 1">上一页</button>
        <span>第 {{ currentPage }} 页</span>
        <button @click="changePage(currentPage + 1)" :disabled="currentPage >= totalPages">下一页</button>
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        products: [],
        filteredProducts: [],
        searchQuery: '',
        currentPage: 1,
        pageSize: 10,
        suggestions: [],
        categories: ["全部", "电子", "家居", "服装"],
        selectedCategory: '全部',
      };
    },
    computed: {
      totalPages() {
        return Math.ceil(this.filteredProducts.length / this.pageSize);
      },
      paginatedProducts() {
        const start = (this.currentPage - 1) * this.pageSize;
        const end = this.currentPage * this.pageSize;
        return this.filteredProducts.slice(start, end);
      },
    },
    mounted() {
      axios.get('/api/products').then((response) => {
        this.products = response.data.data.products;
        this.filteredProducts = [...this.products];
      });
  
      // 监听点击事件，判断是否点击了空白处
      document.addEventListener('click', this.handleClickOutside);
    },
    beforeDestroy() {
      // 组件销毁时移除事件监听器
      document.removeEventListener('click', this.handleClickOutside);
    },
    methods: {
      handleImageLoad(event) {
        const img = event.target;
        img.style.opacity = 1;
      },
      changePage(page) {
        if (page < 1 || page > this.totalPages) return;
        this.currentPage = page;
      },
      filterProducts() {
        let query = this.searchQuery.trim().toLowerCase();
        if (query === '') {
          this.filteredProducts = this.products.filter(product =>
            this.selectedCategory === '全部' || product.category === this.selectedCategory
          );
          this.suggestions = [];
        } else {
          this.filteredProducts = this.products.filter(product =>
            (product.name.toLowerCase().includes(query) || product.category.toLowerCase().includes(query)) &&
            (this.selectedCategory === '全部' || product.category === this.selectedCategory)
          );
  
          this.suggestions = this.products.filter(product =>
            product.name.toLowerCase().includes(query)
          ).slice(0, 5);
        }
        this.currentPage = 1;
      },
      filterByCategory(category) {
        this.selectedCategory = category;
        this.suggestions = []; // 清空搜索提示
        this.filterProducts();   // 重新筛选商品
      },
      selectSuggestion(suggestion) {
        this.searchQuery = suggestion.name;
        this.filterProducts();
        this.suggestions = [];
      },
      handleClickOutside(event) {
        const searchBox = this.$el.querySelector('.search-box');
        const suggestionsList = this.$el.querySelector('.suggestions-list');
        
        // 判断点击是否在搜索框和提示框外部
        if (searchBox && !searchBox.contains(event.target) && suggestionsList && !suggestionsList.contains(event.target)) {
          this.suggestions = []; // 如果点击外部，隐藏提示框
        }
      },
    },
  };
  </script>
  
  <style scoped>
  .product-list {
    padding: 20px;
    background-color: #f4f4f9;
  }
  
  h1 {
    text-align: center;
    font-size: 2em;
    margin-bottom: 20px;
    color: #333;
  }
  
  /* 悬浮分类菜单 */
  .floating-category {
    position: fixed;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    padding: 10px;
    z-index: 999;
  }
  
  .floating-category .category-item {
    display: block;
    margin-bottom: 10px;
    padding: 8px 15px;
    cursor: pointer;
    font-size: 1.1rem;
    border-radius: 30px;
    border: 1px solid #ddd;
    transition: background-color 0.3s ease, color 0.3s ease;
  }
  
  .floating-category .category-item:hover {
    background-color: #007bff;
    color: #fff;
  }
  
  .floating-category .category-item.active {
    background-color: #007bff;
    color: #fff;
  }
  
  /* 搜索框 */
  .search-box {
    margin-bottom: 20px;
    text-align: center;
    position: relative;
  }
  
  .search-input {
    width: 350px;
    padding: 12px;
    font-size: 1.1rem;
    border: 1px solid #ddd;
    border-radius: 30px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: border-color 0.3s ease;
  }
  
  .search-input:focus {
    border-color: #007bff;
    outline: none;
  }
  
  .search-box.active .search-input {
    border-color: #007bff;
  }
  
  /* 提示框样式 */
  .suggestions-list {
    list-style: none;
    padding: 0;
    margin: 0;
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 350px;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    max-height: 200px;
    overflow-y: auto;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    z-index: 999;
  }
  
  .suggestions-list li {
    padding: 10px;
    cursor: pointer;
    transition: background-color 0.2s ease;
  }
  
  .suggestions-list li:hover {
    background-color: #f1f1f1;
  }
  
  /* 商品项样式 */
  .product-item {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 20px;
    border: 1px solid #ddd;
    padding: 20px;
    background-color: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
  }
  
  .product-item:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  }
  
  .product-image {
    width: 200px;
    height: 200px;
    object-fit: cover;
    margin-right: 20px;
    opacity: 1;
    transition: opacity 0.3s ease-in-out;
  }
  
  .product-info {
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  
  .product-info h2 {
    font-size: 1.6em;
    margin: 0 0 10px;
    color: #333;
  }
  
  .product-info p {
    margin: 5px 0;
    color: #666;
  }
  
  .product-info .price {
    font-size: 1.3em;
    color: #e74c3c;
  }
  
  .product-info .category,
  .product-info .stock {
    font-size: 1em;
  }
  
  .product-info .description {
    font-size: 1rem;
    color: #444;
    line-height: 1.5;
    margin-top: 10px;
  }
  
  /* 分页控件 */
  .pagination {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    gap: 10px;
  }
  
  .pagination button {
    padding: 10px 20px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    transition: background-color 0.3s ease;
    font-size: 1rem;
  }
  
  .pagination button:hover {
    background-color: #0056b3;
  }
  
  .pagination button:disabled {
    background-color: #ccc;
    cursor: not-allowed;
  }
  
  .pagination span {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2em;
    color: #333;
  }
  </style>
  